<template>
    <div class="edituserpage">
      <div class="header">
      <van-row>
        <van-col span=12 class="icons">
          <svg @click="goback" width="10.000000" height="16.000000" viewBox="0 0 10 16" fill="none" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
          <path id="Vector" d="M3.63574 8L10 14.2227L8.18213 16L0 8L8.18213 0L10 1.77734L3.63574 8Z" fill-rule="evenodd" fill="#06070D"/>
        </svg>
        </van-col>
      </van-row>
      </div>
      <div class="cgform">
        <van-uploader :after-read="afterRead"  accept="image/*">
        <div class="avater">
          <img class="avaterimg" :src="userimg" />
          <svg class="changeicon" t="1703139693217" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="2838" width="22" height="22"><path d="M684.202667 117.248c15.893333-15.872 42.154667-15.36 58.922666 1.408l90.517334 90.517333c16.661333 16.661333 17.344 42.986667 1.429333 58.922667l-445.653333 445.653333c-7.936 7.914667-23.104 16.746667-34.218667 19.776l-143.701333 39.253334c-21.909333 5.994667-35.114667-7.104-29.568-28.949334l37.248-146.773333c2.773333-10.944 11.562667-26.346667 19.392-34.176l445.653333-445.653333zM268.736 593.066667c-2.901333 2.901333-8.106667 12.074667-9.130667 16.021333l-29.12 114.773333 111.957334-30.570666c4.437333-1.216 13.632-6.549333 16.810666-9.728l445.653334-445.653334-90.517334-90.496-445.653333 445.653334zM682.794667 178.986667l90.517333 90.517333-30.186667 30.186667-90.496-90.517334 30.165334-30.165333z m-362.026667 362.048l90.496 90.517333-30.165333 30.165333-90.517334-90.496 30.165334-30.186666zM170.666667 874.666667c0-11.776 9.429333-21.333333 21.461333-21.333334h661.077333a21.333333 21.333333 0 1 1 0 42.666667H192.128A21.333333 21.333333 0 0 1 170.666667 874.666667z" fill="#ffffff" p-id="2839"></path></svg>
        </div>
      </van-uploader>
      <p style="color: #a4a4a4;">点击更改头像</p>
      <h3 class="fullname">{{ username }}</h3>
      <div class="content">
        <span>请输入用户名</span>
        <input type="text" v-model="username" placeholder="|"/>
      </div>

      <div class="content">
        <span>请输入姓名</span>
        <input type="text" v-model="fullname" placeholder="|"/>
      </div>

      <div class="content">
        <span>请输入邮箱</span>
        <input type="text" v-model="email" placeholder="|"/>
      </div>

      <div class="buybtn" @click="changeUserInfo">
      保存
    </div>
      </div>
    </div>
  </template>

<script>
import { Toast } from 'vant'
import defaultuser from '@/assets/default.jpg'
import { getUserInfo, putUserInfo } from '@/api/api.js'
export default {
  name: 'Home',
  data () {
    return {
      userimg: defaultuser,
      username: '',
      fullname: '',
      imgFile: '',
      email: ''
    }
  },
  methods: {
    goback () {
      this.$router.push('/user')
    },
    afterRead (file) {
      // 此时可以自行将文件上传至服务器
      console.log(file, 'file')
      this.userimg = file.content
      this.imgFile = file
      console.log(this.imgFile.file)
      const formData = new FormData()
      formData.append('avatar', this.imgFile.file)
      putUserInfo(formData).then((res) => {
        console.log(res)
        if (res.code === 200) {
          this.getUserInfo()
        } else {
          Toast.fail('上传失败')
        }
      })
    },
    changeUserInfo () {
      const formData = new FormData()
      formData.append('first_name', this.fullname)
      formData.append('email', this.email)
      formData.append('username', this.username)
      putUserInfo(formData).then((res) => {
        console.log(res)
        if (res.code === 200) {
          Toast.success('修改成功')
          this.loadUserInfo()
        } else {
          Toast.fail('修改失败')
        }
      })
    },
    saveobj () {

    },
    loadUserInfo () {
      getUserInfo().then((res) => {
        if (res.code === 200) {
          this.username = res.data.username
          this.fullname = res.data.first_name
          this.email = res.data.email
          this.userimg = res.data.avatar
        }
      })
    }
  },
  mounted () {
    this.loadUserInfo()
  }
}
</script>
  <style lang="less" scoped>
  .edituserpage{
    padding: 0px 30px;
    width: calc(100% - 60px);
    background: #fdfdfd;
    height: calc(100% - 100px);
    overflow: auto;
  }
  .header{
    height: 60px;
    .icons{
      height: 60px;
      display: flex;
      // justify-content: center;
      align-items: flex-end;
    }
    .rig{
      justify-content: flex-end;
      svg{
        margin: 0px 20px;
      }
    }
  }
  .bookView{
    text-align: center;
    margin: 20px 0px;
    img {
      margin-bottom: 20px;
      width: 211px;
      height: 310px;
      box-shadow: -1px 16px 9px 5px #eee;
    }
    h4{
        margin-bottom: 20px;
    }
    .rate{
      display: block;
      margin-bottom: 20px;
    }
    .intro{
      text-indent: 40px;
      color: #a4a5a7;
      overflow: hidden;
      text-overflow: ellipsis;
      display: -webkit-box;
      -webkit-line-clamp: 3;
      -webkit-box-orient: vertical;
    }
    .allintro{
      text-indent: 40px;
      color: #a4a5a7;
    }
  }
  .fotbtn {
    margin-bottom: 10px;
    height: 40px;
    .infobtn{
      height: 40px;
      background: #ffffff;
      display: flex;
      align-items: center;
      justify-content: center;
      border-radius: 10px;
      box-shadow: 0px 0px 6px 0px #eeeeee;
      svg {
        margin: 0px 10px;
      }
    }
  }
  .buybtn{
    text-align: center;
    color: white;
    height: 60px;
    line-height: 60px;
    font-size: 20px;
    position: absolute;
    left: 50%;
    bottom: 20px;
    transform: translate(-50%,0px);
    width: 90%;
    border-radius: 10px;
    background: #06070d;
  }
  .avater{
  text-align: center;
  padding-top: 50px;
  position: relative;
  margin: 0 auto;
  .avaterimg{
    width: 120px;
    height: 120px;
    border-radius: 50%;
    // border: 1px solid;
    overflow: hidden;
  }
  .changeicon{
    position: absolute;
    bottom: 0;
    left: 20%;
    transform: translate(-50%, 0);
    background: #000;
    border-radius: 50%;
    padding: 5px;
    border: 5px solid #ffffff;
  }
}
.cgform{
    width: 100%;
    text-align: center;
    .fullname{
      margin: 10px 0px;
    }
    .content{
      width: 100%;
      height: 50px;
      border: 1px solid #eee;
      border-radius: 15px;
      margin-bottom: 20px;
      display: flex;
      flex-wrap: wrap;
      padding: 10px;
      span{
        display: block;
        width: 100%;
        color: #a4a4a4;
        text-align: left;
      }
      input{
        width: 100%;
        border: none;
        outline: none;
        background-color: #fdfdfd;
      }
    }
}
.buybtn{
  text-align: center;
  color: white;
  height: 60px;
  line-height: 60px;
  font-size: 20px;
  position: absolute;
  left: 50%;
  bottom: 20px;
  transform: translate(-50%,0px);
  width: 90%;
  border-radius: 10px;
  background: #06070d;
}
  </style>
